package cn.edu.hrbcu.es.invigilatemanagerment.controller;

import cn.edu.hrbcu.es.invigilatemanagerment.pojo.*;
import cn.edu.hrbcu.es.invigilatemanagerment.service.IClazzService;
import cn.edu.hrbcu.es.invigilatemanagerment.service.ICourseService;
import cn.edu.hrbcu.es.invigilatemanagerment.service.ITeacherCourseService;
import cn.edu.hrbcu.es.invigilatemanagerment.service.ITeacherService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/TeacherCourse")
public class TeacherCourseController {
    @Autowired
    private ITeacherCourseService teacherCourseService;
    @Resource
    ITeacherService teacherService;
    @Resource
    private ICourseService courseService;
    @Resource
    private IClazzService clazzService;


    @RequestMapping(value = {"/toAddTeacherCourse"})
    public ModelAndView toAddTeacherCourse() {
        List<Teacher> teachers = teacherService.queryAllTeacher();
        List<Course> courses = courseService.queryAllCourse();
        List<Clazz> clazzes = clazzService.queryAllClazz();

        ModelAndView model = new ModelAndView("toaddteachercourse");
        model.addObject("teachers", teachers);
        model.addObject("courses", courses);
        model.addObject("clazzes", clazzes);

        return model;
    }

    @RequestMapping(value = {"/toUpdateTeacherCourse"})
    public ModelAndView toUpdateTeacherCourse(Integer id) {
        List<Teacher> teachers = teacherService.queryAllTeacher();
        List<Course> courses = courseService.queryAllCourse();
        List<Clazz> clazzes = clazzService.queryAllClazz();

        TeacherCourse teacherCourse = teacherCourseService.queryTeacherCourseById(id);
        ModelAndView model = new ModelAndView("toupdateteachercourse");
        model.addObject("teachers", teachers);
        model.addObject("courses", courses);
        model.addObject("clazzes", clazzes);
        model.addObject("teacherCourse",teacherCourse);
        return model;
    }

    @RequestMapping("/queryAllTeacherCourse")
    public String queryAllTeacherCourse(Model model,@RequestParam(value = "pageNow",defaultValue = "1")int pageNow,
                                        @RequestParam(value = "pageSize",defaultValue = "5")int pageSize)
    {
        int totalCount = teacherCourseService.queryTeacherCourseCount();
        int pageCount = (int)Math.ceil((double)totalCount/pageSize);
        List<Map<String,Object>> teacherCourses = new ArrayList<Map<String,Object>>();
        List<TeacherCourse> teacherCourses_ = teacherCourseService.queryTeacherCourseByPage(pageNow,pageSize);

        if(teacherCourses_ != null && teacherCourses_.size() > 0){
            for (TeacherCourse teacherCourse_ : teacherCourses_) {
                Map<String,Object> teacherCourse = new HashMap<>();
                Teacher teacher = teacherService.queryTeacherById(teacherCourse_.getTeacherId());
                Course course = courseService.queryCourseById(teacherCourse_.getCourseId());
                Clazz clazz = clazzService.queryClazzById(teacherCourse_.getClazzId());

                teacherCourse.put("id",teacherCourse_.getId());
                teacherCourse.put("coursename",course.getName());
                teacherCourse.put("teachername",teacher.getName());
                teacherCourse.put("clazzname",clazz.getName());
                teacherCourse.put("note",teacherCourse_.getNote());
                teacherCourses.add(teacherCourse);
            }
        }
        model.addAttribute("teacherCourses", teacherCourses);
        model.addAttribute("pageCount",pageCount);
        model.addAttribute("pageNow",pageNow);
        model.addAttribute("pageSize",pageSize);
        return "teachercourse";
    }

    @ResponseBody
    @RequestMapping("queryTeacherCourseById")
    public TeacherCourse queryTeacherCourseById(Integer id)
    {
        return teacherCourseService.queryTeacherCourseById(id);
    }

    @ResponseBody
    @RequestMapping("insertTeacherCourse")
    public String insertTeacherCourse(Integer teacherId,Integer courseId,Integer clazzId,String classroom,String note)
    {
        TeacherCourse teacherCourse = new TeacherCourse(null,teacherId,courseId,clazzId,classroom,note);
        teacherCourseService.insertTeacherCourse(teacherCourse);
        return "OK";
    }

    @ResponseBody
    @RequestMapping("updateTeacherCourseById")
    public String updateTeacherCourseById(Integer id,Integer teacherId,Integer courseId,Integer clazzId,String classroom,String note)
    {
        TeacherCourse teacherCourse = new TeacherCourse(id,teacherId,courseId,clazzId,classroom,note);
        teacherCourseService.updateTeacherCourseById(teacherCourse);
        return "OK";
    }

    @ResponseBody
    @RequestMapping("deleteTeacherCourseById")
    public String deleteTeacherCourseById(Integer id)
    {
        teacherCourseService.deleteTeacherCourseById(id);
        return "OK";
    }
}
